package com.funo.wcity.app.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.funo.wcity.app.dao.LoginUserDAO;
import com.funo.wcity.app.dao.TblCompaniesDAO;
import com.funo.wcity.app.dao.TblUserMailDAO;
import com.funo.wcity.app.entity.LoginUser;
import com.funo.wcity.app.entity.TblCompanies;
import com.funo.wcity.app.entity.TblUserMail;
import com.funo.wcity.app.entity.TblUsers;
import com.funo.wcity.app.service.LoginUserService;

@Service("loginUserService")
public class LoginUserServiceImpl implements LoginUserService {
	@Resource(name="loginUserDAO")
    private LoginUserDAO loginUserDAO;
	@Resource(name="tblUserMailDAO")
	private TblUserMailDAO tblUserMailDAO;
	@Resource(name="tblCompaniesDAO")
	private TblCompaniesDAO tblCompaniesDAO;
	public LoginUser findLoginUser(String username, String password) {
		//查询用户
		List list=this.loginUserDAO.getLoginUser(username, password);
		if(list.size()>0){
			 TblUsers tblUsers=(TblUsers)list.get(0);
			 LoginUser 	loginUser=new LoginUser();
			 loginUser.setIdUser(tblUsers.getIdUser());
			 loginUser.setUsername(tblUsers.getUsername());
			 loginUser.setDisplayName(tblUsers.getDisplayName());
			 loginUser.setPassword(tblUsers.getPassword());
			 loginUser.setIdRole(tblUsers.getIdRole());
			 loginUser.setIdEmployee(tblUsers.getIdEmployee());
			 //查询email账号
			 List<TblUserMail> mailList=this.tblUserMailDAO.getTblUserMailList(tblUsers.getIdUser());
			 if(mailList.size()>0){
				 loginUser.setData(mailList);
			 }
			 //属于公司
			 List<TblCompanies> companiesList=this.tblCompaniesDAO.getTblCompaniesList(loginUser.getIdEmployee());
			 if(companiesList.size()>0){
				 loginUser.setIdCompany(companiesList.get(0).getIdCompany());
			 }
			return loginUser;
		}else{
			return null;
		}
	}

	public void saveTblUsers(TblUsers tblUsers) {
		 this.loginUserDAO.save(tblUsers);
	}

	public void insertTblUsers(TblUsers tblUsers) {
		this.loginUserDAO.save(tblUsers);
	}

}
